package com.caimore.module.mc8630;

import com.caimore.common.Serial_For_Smslib;
import com.caimore.common.SmsUtils_sdk;
import com.caimore.entity.SMSMessageMgr;
import com.caimore.entity.SendSmsParam;
import com.caimore.modulefactory.IsmsWriter;
import java.util.ArrayList;

public class ATWriterThreadPDU8630 implements IsmsWriter {

   Serial_For_Smslib SerilaSmslib;
   private volatile boolean shutdown = false;
   private volatile boolean sendMs = false;
   Thread t1 = null;


   public ATWriterThreadPDU8630(Serial_For_Smslib _smslib_test, boolean _shutdown) {
      this.SerilaSmslib = _smslib_test;
      this.shutdown = _shutdown;
   }

   public void start() {
      this.t1 = new Thread() {
         public void run() {
            String e;
            try {
               e = "AT+CPIN ? \r";
               char[] sReceivedNum = e.toCharArray();
               ATWriterThreadPDU8630.this.SerilaSmslib.writeByte(sReceivedNum);
               System.out.println("[ATWriterThreadPDU8630]sSend-AT-Command:" + e);

               while(!SMSMessageMgr.getInstance().getREADY() && !SMSMessageMgr.getInstance().getREADY()) {
                  ;
               }

               if(SMSMessageMgr.getInstance().getREADY()) {
                  String oSendSmsParam = "AT^HSMSSS=0,0,1,0\r";
                  char[] lstSendContext = oSendSmsParam.toCharArray();
                  ATWriterThreadPDU8630.this.SerilaSmslib.writeByte(lstSendContext);
                  System.out.println("[ATWriterThreadPDU8630]sSend-AT-Command: " + oSendSmsParam);
               }
            } catch (Exception var13) {
               System.out.println(var13.getMessage());
            }

            while(!ATWriterThreadPDU8630.this.shutdown) {
               try {
                  if(SMSMessageMgr.getInstance().getREADY()) {
                     e = null;
                     String var14 = null;
                     SendSmsParam var15 = SMSMessageMgr.getInstance().delSendSmsParam();
                     if(var15 != null) {
                        e = var15.getContent();
                        var14 = var15.getPhone();
                     }

                     if(e == null && var14 == null && !ATWriterThreadPDU8630.this.sendMs) {
                        var15 = SMSMessageMgr.getInstance().delSendSmsParam();
                        if(var15 != null) {
                           e = var15.getContent();
                           var14 = var15.getPhone();
                        }
                     } else {
                        ATWriterThreadPDU8630.this.sendMs = true;
                     }

                     ArrayList var16 = new ArrayList();
                     if(e != null) {
                        if(e.length() <= 35) {
                           var16.add(e);
                        } else {
                           while(e.length() > 35) {
                              String i = e.substring(0, 35);
                              var16.add(i);
                              e = e.substring(35);
                              if(e.length() <= 35) {
                                 var16.add(e);
                              }
                           }
                        }

                        int var17 = 0;

                        while(var17 < var16.size()) {
                           String sTempBody = (String)var16.get(var17);

                           for(int j = 0; j < 3; ++j) {
                              System.out.println("SmsUtils.getInstance().getOK(): " + SMSMessageMgr.getInstance().getOK());
                              System.out.println("SmsUtils.getInstance().getCR(): " + SMSMessageMgr.getInstance().getCR());
                              String sHCMGS;
                              if(j == 0) {
                                 sHCMGS = "AT^HCMGS=\"" + var14 + "\"\r";
                                 char[] sCMGS = sHCMGS.toCharArray();
                                 ATWriterThreadPDU8630.this.SerilaSmslib.writeByte(sCMGS);
                                 System.out.println("[ATWriterThreadPDU8630]sSend-AT-Command: " + sHCMGS);
                                 Thread.sleep(2000L);
                              } else if(j == 1) {
                                 byte[] var18 = SmsUtils_sdk.encode8630UCS2(sTempBody);
                                 byte[] var19 = new byte[var18.length + 2];
                                 System.arraycopy(var18, 0, var19, 0, var18.length);
                                 var19[var18.length] = 0;
                                 var19[var18.length + 1] = 26;
                                 ATWriterThreadPDU8630.this.SerilaSmslib.writeByte8332(var19);
                                 System.out.println("[ATWriterThreadPDU8630]sSend-ContextBody:" + new String(var19));
                                 System.out.println("\r\n\r\n");
                                 Thread.sleep(2000L);
                              } else if(j == 2) {
                                 sHCMGS = SMSMessageMgr.getInstance().getHCMGS();
                                 String var20 = "^HCMGS:" + sHCMGS + "\r";
                                 char[] atCMGS = var20.toCharArray();
                                 ATWriterThreadPDU8630.this.SerilaSmslib.writeByte(atCMGS);
                                 System.out.println("[ATWriterThreadPDU8630]sSend-AT-Command: " + var20);
                                 ++var17;
                                 Thread.sleep(2000L);
                              }
                           }

                           Thread.sleep(2000L);
                        }

                        ATWriterThreadPDU8630.this.sendMs = false;
                     }
                  }

                  Thread.sleep(1000L);
               } catch (InterruptedException var11) {
                  Thread.currentThread().interrupt();
               } catch (Exception var12) {
                  System.out.println(var12.getMessage());
               }
            }

         }
      };
      this.t1.start();
   }

   public final void shutdownRequest() {
      this.shutdown = true;
      this.t1.interrupt();
   }
}
